package com.ruoyi.web.exams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.exams.domain.ExamAnswers;
import com.ruoyi.web.exams.domain.ExamPager;
import com.ruoyi.web.exams.domain.ExamResult;
import com.ruoyi.web.exams.dto.ExamUserResult;
import com.ruoyi.web.exams.mapper.*;
import com.ruoyi.web.exams.service.IExamResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 试卷答题结果概览Service业务层处理
 *
 * @author 范书铭
 * @date 2024-10-02
 */
@Service
public class ExamResultServiceImpl extends ServiceImpl<ExamResultMapper, ExamResult> implements IExamResultService {
    @Autowired
    private ExamResultMapper examResultMapper;
    @Autowired
    private ExamPagerMapper examPagerMapper;
    @Resource
    private ExamQuestionMapper examQuestionMapper;

    @Resource
    private ExamAnswersMapper examAnswersMapper;
    @Resource
    private ExamOptionMapper examOptionMapper;
    @Autowired
    private ExamPagerServiceImpl examPagerServiceImpl;

    /**
     * 查询试卷答题结果概览
     *
     * @param id 试卷答题结果概览主键
     * @return 试卷答题结果概览
     */
    @Override
    public ExamResult selectExamResultById(Long id) {
        return examResultMapper.selectExamResultById(id);
    }

    /**
     * 查询试卷答题结果概览列表
     *
     * @param examResult 试卷答题结果概览
     * @return 试卷答题结果概览
     */
    @Override
    public List<ExamResult> selectExamResultList(ExamResult examResult) {
        return examResultMapper.selectExamResultList(examResult);
    }

    /**
     * 新增试卷答题结果概览
     *
     * @param examResult 试卷答题结果概览
     * @return 结果
     */
    @Override
    public int insertExamResult(ExamResult examResult) {
        examResult.setCreateTime(DateUtils.getNowDate());
        return examResultMapper.insertExamResult(examResult);
    }

    /**
     * 修改试卷答题结果概览
     *
     * @param examResult 试卷答题结果概览
     * @return 结果
     */
    @Override
    public int updateExamResult(ExamResult examResult) {
        examResult.setUpdateTime(DateUtils.getNowDate());
        return examResultMapper.updateExamResult(examResult);
    }

    /**
     * 批量删除试卷答题结果概览
     *
     * @param ids 需要删除的试卷答题结果概览主键
     * @return 结果
     */
    @Override
    public int deleteExamResultByIds(Long[] ids) {
        return examResultMapper.deleteExamResultByIds(ids);
    }

    /**
     * 删除试卷答题结果概览信息
     *
     * @param id 试卷答题结果概览主键
     * @return 结果
     */
    @Override
    public int deleteExamResultById(Long id) {
        return examResultMapper.deleteExamResultById(id);
    }


    @Override
    public boolean postPager(Long resultId) {
        ExamResult examResult = examResultMapper.selectById(resultId);
        ExamPager examPager = examPagerMapper.selectById(examResult.getPagerId());
        List<ExamAnswers> answers = examAnswersMapper.selectList(new QueryWrapper<ExamAnswers>()
                .eq("exam_result_id", examResult.getId()));
        examResult.setIsFinish(true);
        examResult.setTotalScore(examPager.getPagerScore());
        float singleQuestionScore = 0f;
        float multiQuestionScore = 0f;
        float judgeScore = 0f;
        for (ExamAnswers examAnswers : answers) {
            if (examAnswers.getUserAnswerStatus() == 2) {
                switch (examAnswers.getQuestionType().intValue()) {
                    case 1:
                        singleQuestionScore += examAnswers.getScoreNumber().floatValue();
                        break;
                    case 2:
                        multiQuestionScore += examAnswers.getScoreNumber().floatValue();
                        break;
                    case 3:
                        judgeScore += examAnswers.getScoreNumber().floatValue();
                        break;
                    default:
                        break;
                }
            }
        }
        examResult.setUserScore(new BigDecimal(singleQuestionScore + multiQuestionScore + judgeScore));
        examResult.setJudgeQuestionScore(new BigDecimal(judgeScore));
        examResult.setSingleQuestionScore(new BigDecimal(singleQuestionScore));
        examResult.setMultiQuestionScore(new BigDecimal(multiQuestionScore));
        examResult.setSubmitTime(new Date());
        return examResultMapper.updateExamResult(examResult) > 0;
    }

    @Override
    public List<ExamUserResult> getUserResults() {

        return examResultMapper.selectUserExamResult();
    }
}